package com.lanou3g.ptp.api.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.service.ApiClazzService;
import com.lanou3g.ptp.entity.Clazz;
import com.lanou3g.ptp.entity.Major;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApiClazzServiceImpl implements ApiClazzService {
    @Reference
    private ClazzService clazzService;
    @Reference
    private MajorService majorService;
    @Reference
    private StudentService studentService;
    @Override
    public PageInfo<Object> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Clazz> clazzes = clazzService.findAll();
        List<Object> list = new ArrayList<>();
        for (Clazz clazz : clazzes) {
            Map<String, Object> result = new HashMap<>();
            result.put("clazz",clazz);
            Major major = majorService.findById(clazz.getMajorId());
            result.put("majorName",major.getName());
            Long count = studentService.findClazzId(clazz.getId());
            result.put("count",count);
            list.add(result);
        }
        return new PageInfo<>(list);
    }

    @Override
    public PtpResult findByName(String name) {
        Clazz clazz = clazzService.findByName(name);
        Map<String, Object> result = new HashMap<>();
        result.put("clazz",clazz);
        Major major = majorService.findById(clazz.getMajorId());
        result.put("majorName",major.getName());
        Long count = studentService.findClazzId(clazz.getId());
        result.put("count",count);
        return PtpResultUtils.ok(result);
    }
}
